package it.polimi.politics.ejb.entities;

import it.polimi.politics.dto.UtentiDTO;
import it.polimi.politics.ejb.entities.Group;

import java.io.Serializable;

import javax.persistence.*;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.Date;
import java.util.List;


/**
 * The persistent class for the utenti database table.
 * 
 */
@Entity
@NamedQueries({
@NamedQuery(name=Utenti.FINDBYPARTITO, query="SELECT u FROM Utenti u WHERE u.partito.idpartito= :idpartito"),		
@NamedQuery(name=Utenti.FIND_ALL, query="SELECT u FROM Utenti u")})

public class Utenti implements Serializable {
        private static final long serialVersionUID = 1L;
        
        public static final String FIND_ALL = "User.findAll";
        public static final String FINDBYPARTITO = "User.findPartito";
        public static final String FIND_BY_ID = "User.findbyid";
        

        @Id
	private String username;

	
	
	private String IDUtente;
	
	private String citta;

	private String codicefiscale;



	private String cognome;


	@Temporal(TemporalType.TIMESTAMP)
	private Date datadinascita;


	private String email;

	private String nome;

	private String password;

	private int tipoutente;


        //bi-directional many-to-many association to Referendum
        @ManyToMany
        @JoinTable(
                        name="partecipazionereferendum"
                        , joinColumns={
                                @JoinColumn(name="IDUtente")
                                }
                        , inverseJoinColumns={
                                @JoinColumn(name="IDReferendum")
                                }
                        )
        private List<Referendum> referendums1;

	//bi-directional many-to-many association to Candidatura
	@ManyToMany
	@JoinTable(
		name="votopercandidatura"
		, joinColumns={
			@JoinColumn(name="IDPolitico")

			}
		, inverseJoinColumns={
			@JoinColumn(name="IDCandidatura")


			}
		)
	private List<Candidatura> candidaturas;
	
	
	

	//bi-directional many-to-one association to Partito
	@ManyToOne
	@JoinColumn(name="IDPARTITO")
	private Partito partito;

        //bi-directional many-to-many association to Referendum
        @ManyToMany
        @JoinTable(
                name="liketable"
                , joinColumns={
                        @JoinColumn(name="IDUtente")
                        }
                , inverseJoinColumns={
                        @JoinColumn(name="IDReferendum")
                        }
                )
        private List<Referendum> referendums2;

        @ElementCollection(targetClass = Group.class)
    @CollectionTable(name = "USERS_GROUP",
                    joinColumns = @JoinColumn(name = "username"))
    @Enumerated(EnumType.STRING)


    @Column(name="groupname")
    private List<Group> groups;









	public Utenti() {
	super();
	}


	        public Utenti(UtentiDTO utente){
        
        this.citta              = utente.getCitta();
        this.codicefiscale      = utente.getCodiceFiscale();
        this.cognome            = utente.getCognome();
        this.username           = utente.getUsername();       
        this.nome               = utente.getNome();
        this.email              = utente.getEMail();
        this.IDUtente 			= utente.getUsername();
        this.tipoutente         = 1;   
        this.password           = DigestUtils.sha256Hex(utente.getPassword());
        this.datadinascita      = utente.getDataDiNascita();
        
        
    }




	public String getUsername() {
		return this.username;
	}




	public void setUsername(String username) {
		this.username = username;
	}



        public String getIDUtente() {
                return this.IDUtente;
        }











        public void setIDUtente(String IDUtente) {
                this.IDUtente = IDUtente;
        }
	public String getCitta() {
		return this.citta;
	}

	public void setCitta(String citta) {


		this.citta = citta;
	}




	public String getCodicefiscale() {
		return this.codicefiscale;
	}




	public void setCodicefiscale(String codicefiscale) {
		this.codicefiscale = codicefiscale;
	}




	public String getCognome() {
		return this.cognome;
	}

	public void setCognome(String cognome) {


		this.cognome = cognome;
	}




	public Date getDatadinascita() {
		return this.datadinascita;
	}




	public void setDatadinascita(Date datadinascita) {
		this.datadinascita = datadinascita;
	}




	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {


		this.email = email;
	}




	public String getNome() {
		return this.nome;
	}

	public void setNome(String nome) {


		this.nome = nome;
	}


	public String getPassword() {
		return this.password;

	}




	public void setPassword(String password) {
		this.password = password;
	}




	public int getTipoutente() {
		return this.tipoutente;
	}




	public void setTipoutente(int tipoutente) {
		this.tipoutente = tipoutente;
	}




	public List<Candidatura> getCandidaturas() {
		return this.candidaturas;
	}




	public void setCandidaturas(List<Candidatura> candidaturas) {
		this.candidaturas = candidaturas;
	}




	public Partito getPartito() {
		return this.partito;
	}




	public void setPartito(Partito partito) {
		this.partito = partito;
	}




	public List<Referendum> getReferendums1() {
		return this.referendums1;
	}

	public void setReferendums1(List<Referendum> referendums1) {


		this.referendums1 = referendums1;
	}

	public List<Referendum> getReferendums2() {
		return this.referendums2;

	}




	public void setReferendums2(List<Referendum> referendums2) {
		this.referendums2 = referendums2;
	}

    public List<Group> getGroups() {
        return groups;
}




public void setGroups(List<Group> groups) {
        this.groups = groups;
}

}
